using System.Collections;
using System.Collections.Generic;

using UnityEngine;

using Plugin.Tool;

public class GroundEnemy : Enemy
{
    private readonly HashSet<States> groundStates = new HashSet<States>() { States.Idle, States.Move };

    protected override void Awake()
    {

        base.Awake();
    }
    protected override void Start()
    {
        base.Start();
    }

    protected override void Update()
    {

        base.Update();

        if (groundStates.Contains(CurrentState))
        {
            if (IsPlayerDetected() || Vector2.Distance(transform.position, player.transform.position) < 4f)
            {
                ChangeState(States.Move);
                ChangeState(States.Battle);
            }
        }
    }


    #region  状态切换

    protected void Idle_Enter()
    {
        idleTime = Random.Range(1f, 2f);
    }
    protected void Idle_Update()
    {
        idleTime -= Time.deltaTime;
        if (idleTime < 0)
        {
            ChangeState(States.Move);
        }
    }

    protected void Move_Enter()
    {
    }
    protected void Move_Update()
    {
        // 撞墙或没路时翻转
        if (IsWallDetected() || !IsGroundDetected())
        {
            Flip();
            ChangeState(States.Idle);
        }
        SetVelocity(currentMoveSpeed * facingDir, rb.velocity.y);
    }


    #region  战斗
    protected void Battle_Update()
    {
        if (!IsPlayerDetected())
        {
            // 玩家离开
            // 超出范围，直接回到idle
            if (Vector2.Distance(transform.position, player.transform.position) > 10f)
            {
                ChangeState(States.Idle);
            }
            // 否则追击一段时间
            if (battleTime == 0)
            {
                ZTimer.ClearTimer(battleTime);
                battleTime = ZTimer.SetTimeout(battleDuration, () =>
                {
                    ChangeState(States.Idle);
                });
            }
        }
        else
        {
            ZTimer.ClearTimer(battleTime);
            battleTime = 0;

            if (IsPlayerDetected().distance < attackDistance)
            {
                if (CanAttack())
                    ChangeState(States.LightDoubleAttack);
            }
        }

        // 向玩家移动，不能贴近玩家
        if (player.transform.position.x > transform.position.x + 1f)
        {
            // facingDir = 1;
            SetVelocity(currentMoveSpeed * 1, rb.velocity.y);
        }
        else if (player.transform.position.x < transform.position.x - 1f)
        {
            // facingDir = -1;
            SetVelocity(currentMoveSpeed * -1, rb.velocity.y);
        }
    }
    #endregion

    #region  攻击
    protected void LightDoubleAttack_AnimationFinishTrigger()
    {
        // fsm.ChangeState(States.Idle);
    }
    protected void LightDoubleAttack_Enter()
    {
        ZeroVelocity();
        anim.SetInteger("LightDoubleAttackCombo", lightDoubleAttackCombo);

        if (stats.lightDoubleAttackMovement.Count > 0 && lightDoubleAttackCombo < stats.lightDoubleAttackMovement.Count)
            SetVelocity(stats.lightDoubleAttackMovement[lightDoubleAttackCombo].x * facingDir, stats.lightDoubleAttackMovement[lightDoubleAttackCombo].y);
    }

    protected void LightDoubleAttack_Exit()
    {
        lightDoubleAttackCombo++;
        if (lightDoubleAttackCombo > lightDoubleAttackComboMax || Time.time >= lastAttackTime + comboWindow)
            lightDoubleAttackCombo = 0;
        lastAttackTime = Time.time;
    }

    protected void LightDoubleAttack_Update()
    {
        if (canBeStunnned)
            GameMgr.Instance.player.fsm.Driver.OnOpenCounter.Invoke(this);
    }
    #endregion



    #endregion


}
